import java.util.HashMap;
import java.util.Map;

/**
 * @author LKQ
 * @date 2021/11/28 10:50
 * @description 罗马数字
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(romanToInt("MCMXCIV"));
    }

    public static int romanToInt(String s) {
        int number = 0;
        Map<String, Integer> map = new HashMap<>();
        map.put("I", 1);
        map.put("V", 5);
        map.put("X", 10);
        map.put("L", 50);
        map.put("C", 100);
        map.put("D", 500);
        map.put("M", 1000);
        for (int i = 0; i < s.length(); i++) {
            String temp = s.substring(i, i+2 > s.length() ? s.length()-1: i+2);
            boolean flag = false;
            switch (temp) {
                case "IV":
                    number += 4;
                    i = i+1;
                    break;
                case "IX":
                    number += 9;
                    i = i+1;
                    break;
                case "XL":
                    number += 40;
                    i = i+1;
                    break;
                case "XC":
                    number += 90;
                    i = i+1;
                    break;
                case "CD":
                    number += 400;
                    i = i+1;
                    break;
                case "CM":
                    number += 900;
                    i = i+1;
                    break;
                default:
                    flag = true;
                    break;
            }
            if (flag) {
                temp = s.substring(i, i+1);
                if (map.containsKey(temp)) {
                    number += map.get(temp);
                }
            }
        }
        return number;
    }
}
